package com.ygqh.baby.service.impl;

import com.mumu.commons.utils.StringUtils;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgAgentLevelMapper;
import com.ygqh.baby.po.YgAgentLevel;
import com.ygqh.baby.po.YgAgentLevelExample;
import com.ygqh.baby.service.YgAgentLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: sunshuo
 * @Date: 2019/9/16/016 16:27
 * @Version: 1.0
 */
@Service
public class YgAgentLevelServiceImpl implements YgAgentLevelService{
	
	@Autowired
	private YgAgentLevelMapper ygAgentLevelMapper;
	
	@Override
	public List<YgAgentLevel> find() {
		YgAgentLevelExample example = new YgAgentLevelExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygAgentLevelMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgAgentLevelExample example = new YgAgentLevelExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygAgentLevelMapper.countByExample(example);
	}

	@Override
	public YgAgentLevel findById(Long id) {
		return ygAgentLevelMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgAgentLevel ygAgentLevel) {
		return ygAgentLevelMapper.insertSelective(ygAgentLevel);
	}

	@Override
	public int update(YgAgentLevel ygAgentLevel) {
		return ygAgentLevelMapper.updateByPrimaryKeySelective(ygAgentLevel);
	}

	@Override
	public int deleteSoft(Long[] ids, String handler) {
		Assert.notEmpty(ids);
		YgAgentLevelExample example = new YgAgentLevelExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgAgentLevel record = new YgAgentLevel();
		record.setStatus(DataStatus.Delete.name());
		record.setUpdateTime(new Date());
		record.setUpdateBy(handler);
		return ygAgentLevelMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id, String handler) {
		return this.deleteSoft(new Long[]{id}, handler);
	}

	@Override
	public int saveOrUpdate(YgAgentLevel ygAgentLevel) {
		int r;
		if (ygAgentLevel.getId() != null) {
			r = this.update(ygAgentLevel);
		} else {
			r = this.save(ygAgentLevel);
		}
		return r;
	}

	@Override
	public ResultSet<YgAgentLevel> search(QueryInfo queryInfo, String q) {
		int count = ygAgentLevelMapper.countSuper(q);
		if (count == 0) {
			return new ResultSet<>();
		}
		List<YgAgentLevel> list = ygAgentLevelMapper.selectSuper(queryInfo, q);
		return new ResultSet<>(count,list);
	}

    @Override
    public Message edit(YgAgentLevel agentLevel) {
		if (StringUtils.isBlank(agentLevel.getLevelName())) {
			return Message.error("等级名称不能为空", null);
		}
		if (agentLevel.getLevelRate() == null) {
			return Message.error("返利比例不能为空", null);
		}
		if (agentLevel.getLevelRate().compareTo(BigDecimal.ZERO) < 0) {
			return Message.error("返利比例不能小于0", null);
		}
		if (ygAgentLevelMapper.existsLevelName(agentLevel.getLevelName(), agentLevel.getId())) {
			return Message.error("等级名称重复", null);
		}
		agentLevel.setUpdateTime(new Date());
		if (agentLevel.getId() == null) {
			agentLevel.setCreateTime(agentLevel.getUpdateTime());
			agentLevel.setCreateBy(agentLevel.getUpdateBy());
		}
        return Message.success(this.saveOrUpdate(agentLevel));
    }

}

